/*
* Copyright (c) 2014 Qunar.com. All Rights Reserved.
*/
package net.oschina.bilbodai.common.beanutil.property.asm.lexer.code.mp;

/**
 * 如果一个属性声明的时候指定了泛型类型，那么根据键的泛型类型返回类型加载实例
 * <br>
 * 如果属性并未声明泛型类型，如只是声明成Map,那么可以通过 '%类型描述字符%key'来显示指定。
 * 比如如果知道Map中的key为Integer，key为100。只需定义为：%int%100 即可。{@link MapKeyTypeMapping}
 *
 * @author qt-maodai  Date: 15-8-8 Time: 下午10:41
 * @version $Id$
 */
public class MapKeyTypes {

    private static final String EXTEND_TYPE_INDICATE_PREFIX = "<";
    private static final String EXTEND_TYPE_INDICATE_SUFFIX = ">";
    private static final String CUSTOM_TYPE_INDICATE_PREFIX = "@";
    private static final String KEY_VALUE_SEPARATOR = ",";
    public static final String DEFAULT_MAP_KEY_TYPE = "string";
    public static final String DEFAULT_MAP_VALUE_TYPE = "object";

    public static MapKeyType getType(String keyType) {

        //检查是否为扩展类型

        MapKeyType extendType = MapKeyTypeMapping.getType(keyType);
        if (extendType != null) {
            return extendType;
        }

        throw new IllegalArgumentException("键类型'" + keyType + "',不支持被'" + keyType +
                "'扩展类型表示，请检查Map是否声明泛型或者是否用'" + EXTEND_TYPE_INDICATE_PREFIX + EXTEND_TYPE_INDICATE_SUFFIX + "'指定正确的扩展类型，"
                + "或者使用'" + EXTEND_TYPE_INDICATE_PREFIX + CUSTOM_TYPE_INDICATE_PREFIX + EXTEND_TYPE_INDICATE_SUFFIX + "'指定自定义类型");
    }

    public static MapKeySplitter splitKey(String keyString) {
        String actualKey = keyString;
        String keyValueType;
        int extendTypeIndexBegin = keyString.indexOf(EXTEND_TYPE_INDICATE_PREFIX);
        if (extendTypeIndexBegin >= 0) {
            int extendTypeIndexEnd = keyString.lastIndexOf(EXTEND_TYPE_INDICATE_SUFFIX);
            if (extendTypeIndexEnd <= 0) {
                throw new IllegalArgumentException("扩展类型结束符'" + EXTEND_TYPE_INDICATE_SUFFIX + "'不能为空，"
                        + "请使用 '" + EXTEND_TYPE_INDICATE_PREFIX + EXTEND_TYPE_INDICATE_SUFFIX + "' 表示起至");
            }
            keyValueType = keyString.substring(extendTypeIndexBegin + 1, extendTypeIndexEnd);
            //说明这种情况是把拓展类型放在最前面，如 <string>name
            if (extendTypeIndexBegin == 0) {
                actualKey = keyString.substring(extendTypeIndexEnd + 1);
            } else {
                //这种就是将拓展类型放在后面 name<string>
                actualKey = keyString.substring(0, extendTypeIndexBegin);
            }
            String keyType = keyValueType;
            String valueType = DEFAULT_MAP_VALUE_TYPE;
            int sepIndex = keyValueType.indexOf(KEY_VALUE_SEPARATOR);
            if (sepIndex > -1) {
                keyType = keyValueType.substring(0, sepIndex);
                valueType = keyValueType.substring(sepIndex + 1);
            }
            return new MapKeySplitter(actualKey, keyType, valueType);
        }

        //如果没有指定类型，则默认为string类型
        return new MapKeySplitter(actualKey, DEFAULT_MAP_KEY_TYPE, DEFAULT_MAP_VALUE_TYPE);
    }

    //.<string>name
    public static class MapKeySplitter {
        private final String key; //name
        private final String keyType; //string
        private final String valueType;

        public MapKeySplitter(String key, String keyType, String valueType) {
            this.key = key;
            this.keyType = keyType;
            this.valueType = valueType;
        }

        public String getKey() {
            return key;
        }

        public String getKeyType() {
            return keyType;
        }

        public String getValueType() {
            return valueType;
        }
    }
}